<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="文章作者的ID" prop="authorId">-->
<!--        <el-input-->
<!--          v-model="queryParams.authorId"-->
<!--          placeholder="请输入文章作者的ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="作者昵称" prop="authorName">-->
<!--        <el-input-->
<!--          v-model="queryParams.authorName"-->
<!--          placeholder="请输入作者昵称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="文章所属频道ID" prop="channelId">-->
<!--        <el-input-->
<!--          v-model="queryParams.channelId"-->
<!--          placeholder="请输入文章所属频道ID"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="频道名称" prop="channelName">
        <el-input
          v-model="queryParams.channelName"
          placeholder="请输入频道名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="文章布局 0 无图文章 1 单图文章 2多图文章" prop="layout">-->
<!--        <el-input-->
<!--          v-model="queryParams.layout"-->
<!--          placeholder="请输入文章布局 0 无图文章 1 单图文章 2多图文章"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="文章标记 0 普通文章 1热点文章 2 置顶文章 3精品文章 4 大V文章" prop="flag">-->
<!--        <el-input-->
<!--          v-model="queryParams.flag"-->
<!--          placeholder="请输入文章标记 0 普通文章 1热点文章 2 置顶文章 3精品文章 4 大V文章"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="点赞数量" prop="likes">-->
<!--        <el-input-->
<!--          v-model="queryParams.likes"-->
<!--          placeholder="请输入点赞数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="收藏数量" prop="collection">-->
<!--        <el-input-->
<!--          v-model="queryParams.collection"-->
<!--          placeholder="请输入收藏数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="评论数量" prop="comment">-->
<!--        <el-input-->
<!--          v-model="queryParams.comment"-->
<!--          placeholder="请输入评论数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="阅读数量" prop="views">-->
<!--        <el-input-->
<!--          v-model="queryParams.views"-->
<!--          placeholder="请输入阅读数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="省市" prop="provinceId">-->
<!--        <el-input-->
<!--          v-model="queryParams.provinceId"-->
<!--          placeholder="请输入省市"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="市区" prop="cityId">-->
<!--        <el-input-->
<!--          v-model="queryParams.cityId"-->
<!--          placeholder="请输入市区"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="区县" prop="countyId">-->
<!--        <el-input-->
<!--          v-model="queryParams.countyId"-->
<!--          placeholder="请输入区县"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建时间" prop="createdTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.createdTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择创建时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="发布时间" prop="publishTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.publishTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择发布时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="同步状态" prop="syncStatus">-->
<!--        <el-input-->
<!--          v-model="queryParams.syncStatus"-->
<!--          placeholder="请输入同步状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="来源" prop="origin">-->
<!--        <el-input-->
<!--          v-model="queryParams.origin"-->
<!--          placeholder="请输入来源"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="静态网址" prop="staticUrl">-->
<!--        <el-input-->
<!--          v-model="queryParams.staticUrl"-->
<!--          placeholder="请输入静态网址"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tot:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tot:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['tot:article:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tot:article:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="文章作者的ID" align="center" prop="authorId" />
      <el-table-column label="作者昵称" align="center" prop="authorName" />
      <el-table-column label="文章所属频道ID" align="center" prop="channelId" />
      <el-table-column label="频道名称" align="center" prop="channelName" />
      <el-table-column label="文章布局" align="center" prop="layout" #default="scp">
        <span v-if="scp.row.layout==0">无图文章</span>
        <span v-if="scp.row.layout==1">单图文章</span>
        <span v-if="scp.row.layout==2">多图文章</span>
      </el-table-column>
      <el-table-column label="文章标记" align="center" prop="flag" #default="scp">
        <span v-if="scp.row.flag==0">普通文章</span>
        <span v-if="scp.row.flag==1">热点文章</span>
        <span v-if="scp.row.flag==2">置顶文章</span>
        <span v-if="scp.row.flag==3">精品文章</span>
        <span v-if="scp.row.flag==4">大V文章</span>
      </el-table-column>
      <el-table-column label="文章图片" align="center" prop="images" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.images" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="文章标签" align="center" prop="labels" />
      <el-table-column label="点赞数量" align="center" prop="likes" />
      <el-table-column label="收藏数量" align="center" prop="collection" />
      <el-table-column label="评论数量" align="center" prop="comment" />
      <el-table-column label="阅读数量" align="center" prop="views" />
<!--      <el-table-column label="省市" align="center" prop="provinceId" />-->
<!--      <el-table-column label="市区" align="center" prop="cityId" />-->
<!--      <el-table-column label="区县" align="center" prop="countyId" />-->
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="同步状态" align="center" prop="syncStatus" #default="scp">
        <span v-if="scp.row.syncStatus==0">待发布</span>
        <span v-if="scp.row.syncStatus==1">已发布</span>
      </el-table-column>
      <el-table-column label="来源" align="center" prop="origin" />
      <el-table-column label="静态网址" align="center" prop="staticUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tot:article:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tot:article:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改App文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="文章作者的ID" prop="authorId">
          <el-input v-model="form.authorId" placeholder="请输入文章作者的ID" />
        </el-form-item>
        <el-form-item label="作者昵称" prop="authorName">
          <el-input v-model="form.authorName" placeholder="请输入作者昵称" />
        </el-form-item>
        <el-form-item label="文章所属频道ID" prop="channelId">
          <el-input v-model="form.channelId" placeholder="请输入文章所属频道ID" />
        </el-form-item>
        <el-form-item label="频道名称" prop="channelName">
          <el-input v-model="form.channelName" placeholder="请输入频道名称" />
        </el-form-item>
        <el-form-item label="文章布局" prop="layout">
          <el-input v-model="form.layout" placeholder="请输入文章布局 0 无图文章 1 单图文章 2多图文章" />
        </el-form-item>
        <el-form-item label="文章标记" prop="flag">
          <el-input v-model="form.flag" placeholder="请输入文章标记 0 普通文章 1热点文章 2 置顶文章 3精品文章 4 大V文章" />
        </el-form-item>
        <el-form-item label="文章图片" prop="images">
          <image-upload v-model="form.images"/>
        </el-form-item>
        <el-form-item label="文章标签" prop="labels">
          <el-input v-model="form.labels" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="点赞数量" prop="likes">
          <el-input v-model="form.likes" placeholder="请输入点赞数量" />
        </el-form-item>
        <el-form-item label="收藏数量" prop="collection">
          <el-input v-model="form.collection" placeholder="请输入收藏数量" />
        </el-form-item>
        <el-form-item label="评论数量" prop="comment">
          <el-input v-model="form.comment" placeholder="请输入评论数量" />
        </el-form-item>
        <el-form-item label="阅读数量" prop="views">
          <el-input v-model="form.views" placeholder="请输入阅读数量" />
        </el-form-item>
<!--        <el-form-item label="省市" prop="provinceId">-->
<!--          <el-input v-model="form.provinceId" placeholder="请输入省市" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="市区" prop="cityId">-->
<!--          <el-input v-model="form.cityId" placeholder="请输入市区" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="区县" prop="countyId">-->
<!--          <el-input v-model="form.countyId" placeholder="请输入区县" />-->
<!--        </el-form-item>-->
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable

            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable
            v-model="form.publishTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="同步状态" prop="syncStatus">
          <el-input v-model="form.syncStatus" placeholder="请输入同步状态" />
        </el-form-item>
        <el-form-item label="来源" prop="origin">
          <el-input v-model="form.origin" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="静态网址" prop="staticUrl">
          <el-input v-model="form.staticUrl" placeholder="请输入静态网址" />
        </el-form-item>
        <el-form-item label="文章内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle } from "@/api/tot/article";

export default {
  name: "Article",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // App文章表格数据
      articleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        authorId: null,
        authorName: null,
        channelId: null,
        channelName: null,
        layout: null,
        flag: null,
        images: null,
        labels: null,
        likes: null,
        collection: null,
        comment: null,
        views: null,
        provinceId: null,
        cityId: null,
        countyId: null,
        createdTime: null,
        publishTime: null,
        syncStatus: null,
        origin: null,
        staticUrl: null,
        content:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询App文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        authorId: null,
        authorName: null,
        channelId: null,
        channelName: null,
        layout: null,
        flag: null,
        images: null,
        labels: null,
        likes: null,
        collection: null,
        comment: null,
        views: null,
        provinceId: null,
        cityId: null,
        countyId: null,
        createdTime: null,
        publishTime: null,
        syncStatus: null,
        origin: null,
        staticUrl: null,
        content:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加App文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getArticle(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改App文章";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArticle(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArticle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除App文章编号为"' + ids + '"的数据项？').then(function() {
        return delArticle(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tot/article/export', {
        ...this.queryParams
      }, `article_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
